package control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

import model.Device;

import view.InfoGUI;
import view.IpGUI;
import view.TreeGUI;

public class MainListener implements ActionListener, KeyListener, TreeSelectionListener, ListSelectionListener {
	//TESTE	
	private static int id = 0;
	
	private IpGUI ipGUI;
	private TreeGUI treeGUI;
	private InfoGUI infoGUI;
	
	public MainListener(IpGUI ipGUI, TreeGUI treeGUI, InfoGUI infoGUI) {
		this.ipGUI = ipGUI;
		this.treeGUI = treeGUI;
		this.infoGUI = infoGUI;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		//TESTE
		if (e.getSource() == ipGUI.getButtonAddDevice()) {
			Device device = new Device(""+(++id));
			treeGUI.addNodeDevice(device);
		}
	}

	@Override
	public void valueChanged(TreeSelectionEvent e) {
		if (e.getSource() == treeGUI.getTreeDevice()) {
			DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeGUI.getTreeDevice().getLastSelectedPathComponent();
			if (node != null) {
				Object selectedDevice = node.getUserObject();
				if (selectedDevice != null && selectedDevice instanceof Device) {
					infoGUI.addDeviceInfo((Device)selectedDevice);
				}
			}
		}
	}
	
	@Override
	public void valueChanged(ListSelectionEvent e) {
		
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
	}
	
}
